<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/31
 * Time: 15:03
 */
namespace app\service;

use think\Loader;

class SMS {

    protected $accountSid;
    protected $accountToken;
    protected $appId;
    protected $serverIP='app.cloopen.com';
    protected $serverPort='8883';
    protected $softVersion='2013-12-26';

    public function sendTemplateSMS($to,$datas,$tempId){
        Loader::import('sms.REST');
        $this->accountSid = config('smsconfig.accountSid');
        $this->accountToken = config('smsconfig.accountToken');
        $this->appId = config('smsconfig.appId');
        $rest = new \REST($this->serverIP,$this->serverPort,$this->softVersion);
        $rest->setAccount($this->accountSid,$this->accountToken);
        $rest->setAppId($this->appId);

        // 发送模板短信
//        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
//         break;
        }
        if($result->statusCode!=0) {
            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
            return false;
        }else{
//            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $code = $result->statusCode;
            $smsmessage = $result->TemplateSMS;
//            echo "code:".$code."<br/>";
//            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
//            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            return true;
            //TODO 添加成功处理逻辑
        }
    }
}